﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Data.Linq;
using System.Diagnostics;

namespace JsonTest1.DatabaseFramwork
{
    public class MyViewModel
    {
        DatabaseContext _DB;
        public DatabaseContext DB
        {
            get
            {
                return _DB;
            }
            set
            {
                _DB = value;
            }
        }

        private bool IsAddedToDB = true;
        private static MyViewModel _Instance;

         public static MyViewModel GetInstance()
        {
            if (_Instance == null)
                _Instance = new MyViewModel();
            return _Instance;
        }

         private MyViewModel()
        {
            this.Items = new ObservableCollection<MyTable>();
            _DB = new DatabaseContext();
            if (!_DB.DatabaseExists())
                _DB.CreateDatabase();
        }

        public ObservableCollection<MyTable> Items { get; private set; }

        public void SelectData()
        {

            if (IsAddedToDB)
            {

                if (_DB.DatabaseExists())
                {

                    IEnumerator<MyTable> enumerator = _DB.Rows.GetEnumerator();

                    while (enumerator.MoveNext())
                    {

                        this.Items.Add(enumerator.Current);

                    }
                    Debug.WriteLine(this.Items.Count+"***************************");


                    IsAddedToDB = false;



                }

            }



        }



        public bool SaveData(MyTable table)
        {

            try
            {

                this.Items.Add(table);

                _DB.Rows.InsertOnSubmit(table);

                _DB.SubmitChanges();

            }

            catch (Exception e)
            {

                return false;

            }



            return true;

        }



        public bool DeleteData(MyTable table)
        {

            try
            {

                if (_DB.DatabaseExists())
                {

                    _DB.Rows.DeleteOnSubmit(table);

                    _DB.SubmitChanges();

                    this.Items.Remove(table);

                }

            }

            catch (Exception e)
            {

                return false;

            }



            return true;

        }



        public bool UpdateData(MyTable source, MyTable dest)
        {

            try
            {

                var tables = from item in this.Items where (int)item.Index == source.Index select item;



                foreach (MyTable mt in tables)
                {

                    mt.Name = dest.Name;

                  /*  mt.Age = dest.Age;

                    mt.Gen = dest.Gen;*/

                    break;

                }



                MyTable table = _DB.Rows.GetOriginalEntityState(source);

                table.Name = dest.Name;


                _DB.SubmitChanges();

            }

            catch (Exception e)
            {

                return false;

            }



            return true;

        }


    }
}
